{
  "cells": [
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "v0to-QXCQjsm"
      },
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/master/learn/generation/openai/openai-ml-qa/01-making-queries.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/learn/generation/openai/openai-ml-qa/01-making-queries.ipynb)\n",
        "\n",
        "# Making Queries\n",
        "\n",
        "In this notebook we will learn how to query relevant contexts to our queries from Pinecone, and pass these to a generative OpenAI model to generate an answer backed by real data sources. Required installs for this notebook are:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "VpMvHAYRQf9N",
        "outputId": "0b0831f1-a21d-48cd-d9a3-3e24712e48ef"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\u001b[?25l     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/72.0 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m72.0/72.0 kB\u001b[0m \u001b[31m3.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m177.2/177.2 kB\u001b[0m \u001b[31m9.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m35.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m60.0/60.0 kB\u001b[0m \u001b[31m8.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m283.7/283.7 kB\u001b[0m \u001b[31m34.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m114.5/114.5 kB\u001b[0m \u001b[31m15.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m268.8/268.8 kB\u001b[0m \u001b[31m36.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m149.6/149.6 kB\u001b[0m \u001b[31m21.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ],
      "source": [
        "!pip install -qU \\\n",
        "  openai==0.27.7 \\\n",
        "  pinecone-client==2.2.1"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "---\n",
        "\n",
        "🚨 _Note: the above `pip install` is formatted for Jupyter notebooks. If running elsewhere you may need to drop the `!`._\n",
        "\n",
        "---"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "sG6Rm7y-Qw8Y"
      },
      "source": [
        "## Initializing Everything\n",
        "\n",
        "We will start by initializing everything we will be using. Those components are:\n",
        "\n",
        "* Pinecone vector DB for retrieval (we must also connect to the previously build `openai-ml-qa` index)\n",
        "\n",
        "* OpenAI `text-embedding-ada-002` embedding model for embedding queries\n",
        "\n",
        "* OpenAI `text-davinci-003` generation model for generating answers\n",
        "\n",
        "We first initialize the vector DB. For that we need our [free Pinecone API key](https://app.pinecone.io)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "nWA2HdEIadOJ",
        "outputId": "b138c13e-1dc7-4af2-f9ab-627b112aef0f"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.10/dist-packages/pinecone/index.py:4: TqdmExperimentalWarning: Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
            "  from tqdm.autonotebook import tqdm\n"
          ]
        },
        {
          "data": {
            "text/plain": [
              "WhoAmIResponse(username='c78f2bd', user_label='default', projectname='9a4fbb6')"
            ]
          },
          "execution_count": 2,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "import os\n",
        "from pinecone import Pinecone\n",
        "\n",
        "# initialize connection to pinecone (get API key at app.pinecone.io)\n",
        "api_key = os.getenv(\"PINECONE_API_KEY\") or \"PINECONE_API_KEY\"\n",
        "# find your environment in the console next to your api key\n",
        "env = os.getenv(\"PINECONE_ENVIRONMENT\") or \"PINECONE_ENVIRONMENT\"\n",
        "\n",
        "pc = Pinecone(api_key=api_key)\n",
        "pinecone.whoami()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "z3HN5IAHadOJ",
        "tags": [
          "parameters"
        ]
      },
      "outputs": [],
      "source": [
        "index_name = 'openai-ml-qa'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "UPNwQTH0RNcl",
        "outputId": "dca72382-60c5-4089-addf-9aed7fe6d358"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'dimension': 1536,\n",
              " 'index_fullness': 0.0,\n",
              " 'namespaces': {'': {'vector_count': 5458}},\n",
              " 'total_vector_count': 5458}"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# connect to index\n",
        "index = pinecone.Index(index_name)\n",
        "# view index stats\n",
        "index.describe_index_stats()"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "1t2-Z3hYR_I9"
      },
      "source": [
        "Now initialize the OpenAI models (or _\"engines\"_), for this we need an [OpenAI API key](https://platform.openai.com/)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "O3V8sm-3Rw98",
        "outputId": "72960f22-ef33-4b9f-ce4c-900b2584410c"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "<OpenAIObject list at 0x7fba8339c1d0> JSON: {\n",
              "  \"data\": [\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"whisper-1\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-internal\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"babbage\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"davinci\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-davinci-edit-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-davinci-003\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-internal\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"babbage-code-search-code\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-similarity-babbage-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"code-davinci-edit-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-davinci-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"ada\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"babbage-code-search-text\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"gpt-4-0314\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"babbage-similarity\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"code-search-babbage-text-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-curie-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"code-search-babbage-code-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-ada-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-embedding-ada-002\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-internal\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-similarity-ada-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"curie-instruct-beta\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"ada-code-search-code\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"ada-similarity\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"gpt-4\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"code-search-ada-text-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-search-ada-query-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"davinci-search-document\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"ada-code-search-text\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-search-ada-doc-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"davinci-instruct-beta\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-similarity-curie-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"code-search-ada-code-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"ada-search-query\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-search-davinci-query-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"curie-search-query\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"davinci-search-query\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"babbage-search-document\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"ada-search-document\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-search-curie-query-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-search-babbage-doc-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"gpt-3.5-turbo\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"curie-search-document\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-search-curie-doc-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"babbage-search-query\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-babbage-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-search-davinci-doc-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-search-babbage-query-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"curie-similarity\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"gpt-3.5-turbo-0301\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"curie\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-similarity-davinci-001\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"text-davinci-002\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    },\n",
              "    {\n",
              "      \"created\": null,\n",
              "      \"id\": \"davinci-similarity\",\n",
              "      \"object\": \"engine\",\n",
              "      \"owner\": \"openai-dev\",\n",
              "      \"permissions\": null,\n",
              "      \"ready\": true\n",
              "    }\n",
              "  ],\n",
              "  \"object\": \"list\"\n",
              "}"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "import openai\n",
        "\n",
        "# get API key from top-right dropdown on OpenAI website\n",
        "openai.api_key = os.getenv(\"OPENAI_API_KEY\") or \"OPENAI_API_KEY\"\n",
        "\n",
        "openai.Engine.list()  # check we have authenticated"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "L7XlmdH8TPml"
      },
      "source": [
        "We will use the embedding model `text-embedding-ada-002` like so:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "92NmGGJ1TKQp"
      },
      "outputs": [],
      "source": [
        "embed_model = \"text-embedding-ada-002\"\n",
        "\n",
        "query = 'What are the differences between PyTorch and TensorFlow?'\n",
        "\n",
        "res = openai.Embedding.create(\n",
        "    input=[query],\n",
        "    engine=embed_model\n",
        ")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "C8KaNCXUTkI-"
      },
      "source": [
        "And use the returned query vector to query Pinecone like so:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "owQD1-m4Tjuw",
        "outputId": "d7e3af4b-b183-48fa-fda2-932fdafd2aa7"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'matches': [{'id': '3626',\n",
              "              'metadata': {'category': 'General Discussion',\n",
              "                           'context': 'I think this post might help you:\\n'\n",
              "                                      '  \\n'\n",
              "                                      '      \\n'\n",
              "                                      '\\n'\n",
              "                                      '      Medium – 8 Jun 21\\n'\n",
              "                                      '  \\n'\n",
              "                                      '\\n'\n",
              "                                      '  \\n'\n",
              "                                      '    \\n'\n",
              "                                      '\\n'\n",
              "                                      'Pytorch vs Tensorflow 2021 7\\n'\n",
              "                                      '\\n'\n",
              "                                      '  Tensorflow/Keras & Pytorch are by far '\n",
              "                                      'the 2 most popular major machine '\n",
              "                                      'learning libraries. Tensorflow is '\n",
              "                                      'maintained and released by Google while '\n",
              "                                      'Pytorch is maintained and released by '\n",
              "                                      'Facebook. In…\\n'\n",
              "                                      '\\n'\n",
              "                                      '  \\n'\n",
              "                                      '    Reading time: 5 min read\\n'\n",
              "                                      '  \\n'\n",
              "                                      '\\n'\n",
              "                                      '  \\n'\n",
              "                                      '\\n'\n",
              "                                      '  \\n'\n",
              "                                      '    \\n'\n",
              "                                      '    \\n'\n",
              "                                      '  \\n'\n",
              "                                      '\\n'\n",
              "                                      '  \\n'\n",
              "                                      '\\n'\n",
              "                                      '\\n'\n",
              "                                      'If you ask me for my personal opinion I '\n",
              "                                      'find Tensorflow more convenient in the '\n",
              "                                      'industry (prototyping, deployment and '\n",
              "                                      'scalability is easier) and PyTorch more '\n",
              "                                      'handy in research (its more pythonic '\n",
              "                                      'and it is easier to implement complex '\n",
              "                                      'stuff). I encourage you to learn both !',\n",
              "                           'docs': 'tensorflow',\n",
              "                           'href': 'https://discuss.tensorflow.org/t/how-is-pytorch-different-from-tensorflow/5024',\n",
              "                           'marked': 0.0,\n",
              "                           'question': 'Hi Please give me reply?',\n",
              "                           'thread': 'How is PyTorch different from '\n",
              "                                     'TensorFlow?'},\n",
              "              'score': 0.888854802,\n",
              "              'values': []},\n",
              "             {'id': '3492',\n",
              "              'metadata': {'category': 'General Discussion',\n",
              "                           'context': 'So TensorFlow.js has several unique '\n",
              "                                      'advantages over Python equivalent as it '\n",
              "                                      'can run on the client side too, not '\n",
              "                                      'just the server side (via Node) and on '\n",
              "                                      'the server side it can potentially run '\n",
              "                                      'faster than Python due to the JIT '\n",
              "                                      'compiler of JS. Other than that the '\n",
              "                                      'APIs etc are similar and Python is '\n",
              "                                      'older so is more mature in terms of '\n",
              "                                      'development as we are a younger team, '\n",
              "                                      'but growing fast.\\n'\n",
              "                                      'Some links for your consideration:\\n'\n",
              "                                      'Check this video for an overview:\\n'\n",
              "                                      'Web ML: TensorFlow.js in 30 minutes - '\n",
              "                                      'everything you need to know in 2021 '\n",
              "                                      '#MadeWithTFJS\\n'\n",
              "                                      '\\n'\n",
              "                                      'Client side benefits - you can not get '\n",
              "                                      'these server side - unique vs Python:\\n'\n",
              "                                      '\\n'\n",
              "                                      'Privacy\\n'\n",
              "                                      '\\n'\n",
              "                                      'You can both train and classify data on '\n",
              "                                      'the client machine without ever sending '\n",
              "                                      'data to a 3rd party web server. There '\n",
              "                                      'may be times where this may be a '\n",
              "                                      'requirement to comply with local laws, '\n",
              "                                      'such as GDPR for example, or when '\n",
              "                                      'processing any data that the user may '\n",
              "                                      'want to keep on their machine and not '\n",
              "                                      'sent to a 3rd party.\\n'\n",
              "                                      '\\n'\n",
              "                                      'Speed\\n'\n",
              "                                      '\\n'\n",
              "                                      'As you are not having to send data to a '\n",
              "                                      'remote server, inference (the act of '\n",
              "                                      'classifying the data) can be faster. '\n",
              "                                      'Even better, you have direct access to '\n",
              "                                      'the device’s sensors such as the '\n",
              "                                      'camera, microphone, GPS, accelerometer '\n",
              "                                      'and more should the user grant you '\n",
              "                                      'access.\\n'\n",
              "                                      '\\n'\n",
              "                                      'Reach and scale\\n'\n",
              "                                      '\\n'\n",
              "                                      'With one click anyone in the world can '\n",
              "                                      'click a link you send them, open the '\n",
              "                                      'web page in their browser, and utilise '\n",
              "                                      'what you have made. No need for a '\n",
              "                                      'complex server side Linux setup with '\n",
              "                                      'CUDA drivers and much more just to use '\n",
              "                                      'the machine learning system.\\n'\n",
              "                                      '\\n'\n",
              "                                      'Cost\\n'\n",
              "                                      '\\n'\n",
              "                                      'No servers means the only thing you '\n",
              "                                      'need to pay for is a CDN to host your '\n",
              "                                      'HTML, CSS, JS, and model files. The '\n",
              "                                      'cost of a CDN is much cheaper than '\n",
              "                                      'keeping a server (potentially with a '\n",
              "                                      'graphics card attached) running 24/7.\\n'\n",
              "                                      'Speed:\\n'\n",
              "                                      '  \\n'\n",
              "                                      '      \\n'\n",
              "                                      '\\n'\n",
              "                                      '      blog.tensorflow.org\\n'\n",
              "                                      '  \\n'\n",
              "                                      '\\n'\n",
              "                                      '  \\n'\n",
              "                                      '    \\n'\n",
              "                                      '\\n'\n",
              "                                      'How Hugging Face achieved a 2x '\n",
              "                                      'performance boost for Question '\n",
              "                                      'Answering with... 12\\n'\n",
              "                                      '\\n'\n",
              "                                      '  The TensorFlow blog contains regular '\n",
              "                                      'news from the TensorFlow team and the '\n",
              "                                      'community, with articles on Python, '\n",
              "                                      'TensorFlow.js, TF Lite, TFX, and more.\\n'\n",
              "                                      '\\n'\n",
              "                                      '\\n'\n",
              "                                      '  \\n'\n",
              "                                      '\\n'\n",
              "                                      '  \\n'\n",
              "                                      '    \\n'\n",
              "                                      '    \\n'\n",
              "                                      '  \\n'\n",
              "                                      '\\n'\n",
              "                                      '  \\n'\n",
              "                                      '\\n'\n",
              "                                      '\\n'\n",
              "                                      'Which university  / professor is '\n",
              "                                      'teaching TFJS out of curiosity?',\n",
              "                           'docs': 'tensorflow',\n",
              "                           'href': 'https://discuss.tensorflow.org/t/tensorflow-js-and-py-whats-the-difference/6076',\n",
              "                           'marked': 0.0,\n",
              "                           'question': 'Hello! I know how to code '\n",
              "                                       'TensorFlow.py but my uni is teaching '\n",
              "                                       'TensorFlow.js is there any to much '\n",
              "                                       'difference between the two. I dont '\n",
              "                                       'want to learn .js when I already know '\n",
              "                                       '.py. Is one better over the other?',\n",
              "                           'thread': 'TensorFlow .js and .py Whats the '\n",
              "                                     'difference?'},\n",
              "              'score': 0.861814737,\n",
              "              'values': []},\n",
              "             {'id': '992',\n",
              "              'metadata': {'category': 'Course',\n",
              "                           'context': 'Hi Lenn! All the sections have a '\n",
              "                                      'TensorFlow version. Chapter 5 is '\n",
              "                                      'completely framework agnostic, that’s '\n",
              "                                      'why you don’t see any differences '\n",
              "                                      'between the two, but if you look at '\n",
              "                                      'chapter 7, you will see the content is '\n",
              "                                      'very different.',\n",
              "                           'docs': 'huggingface',\n",
              "                           'href': 'https://discuss.huggingface.co/t/tensorflow-in-part-2-of-the-course/11728',\n",
              "                           'marked': 0.0,\n",
              "                           'question': 'Hi,\\n'\n",
              "                                       'I’m doing the second part of the '\n",
              "                                       'course now, in particular, the chapter '\n",
              "                                       '“The  Datasets library”. In Part 1, I '\n",
              "                                       'was following the tensorflow option '\n",
              "                                       'but it seems that now only the pytorch '\n",
              "                                       'one is available (when I select '\n",
              "                                       'tensorflow, it still shows the '\n",
              "                                       'pytorch-based tutorial). Are you '\n",
              "                                       'planning to release the tensorflow '\n",
              "                                       'tutorial for Part 2 also?\\n'\n",
              "                                       'Thanks in advance!',\n",
              "                           'thread': 'Tensorflow in Part 2 of the course'},\n",
              "              'score': 0.834650934,\n",
              "              'values': []}],\n",
              " 'namespace': ''}"
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "xq = res['data'][0]['embedding']\n",
        "\n",
        "res = index.query(vector=xq, top_k=3, include_metadata=True)\n",
        "res"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "3D0cD17cvNr3"
      },
      "source": [
        "We have some relevant contexts there, and some irrelevant. Now we rely on the generative model `text-davinci-003` to generate our answer."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "id": "psbNjUwjvcNZ"
      },
      "outputs": [],
      "source": [
        "limit = 3750\n",
        "\n",
        "contexts = [\n",
        "    x['metadata']['context'] for x in res['matches']\n",
        "]\n",
        "\n",
        "# build our prompt with the retrieved contexts included\n",
        "prompt_start = (\n",
        "    \"Answer the question based on the context below.\\n\\n\"+\n",
        "    \"Context:\\n\"\n",
        ")\n",
        "prompt_end = (\n",
        "    f\"\\n\\nQuestion: {query}\\nAnswer:\"\n",
        ")\n",
        "# append contexts until hitting limit\n",
        "for i in range(1, len(contexts)):\n",
        "    if len(\"\\n\\n---\\n\\n\".join(contexts[:i])) >= limit:\n",
        "        prompt = (\n",
        "            prompt_start +\n",
        "            \"\\n\\n---\\n\\n\".join(contexts[:i-1]) +\n",
        "            prompt_end\n",
        "        )\n",
        "        break\n",
        "    elif i == len(contexts)-1:\n",
        "        prompt = (\n",
        "            prompt_start +\n",
        "            \"\\n\\n---\\n\\n\".join(contexts) +\n",
        "            prompt_end\n",
        "        )\n",
        "\n",
        "# now query text-davinci-003\n",
        "res = openai.Completion.create(\n",
        "    engine='text-davinci-003',\n",
        "    prompt=prompt,\n",
        "    temperature=0,\n",
        "    max_tokens=400,\n",
        "    top_p=1,\n",
        "    frequency_penalty=0,\n",
        "    presence_penalty=0,\n",
        "    stop=None\n",
        ")"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "Iv0EXQdZ1Qy5"
      },
      "source": [
        "We check the generated response like so:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 53
        },
        "id": "kxU8o1zJ1RJP",
        "outputId": "9f5ad185-51ff-466e-d844-1359757b79cd"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "'If you ask me for my personal opinion I find Tensorflow more convenient in the industry (prototyping, deployment and scalability is easier) and PyTorch more handy in research (its more pythonic and it is easier to implement complex stuff).'"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "res['choices'][0]['text'].strip()"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "YiFnqQ-S3XhU"
      },
      "source": [
        "What we get here essentially an extract from the top result, we can ask for more information by modifying the prompt."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 124
        },
        "id": "WgiX24OV3OOm",
        "outputId": "9df2f3bc-572a-4498-d291-b42a73e34a16"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "'PyTorch and TensorFlow are two of the most popular major machine learning libraries. TensorFlow is maintained and released by Google while PyTorch is maintained and released by Facebook. TensorFlow is more convenient in the industry for prototyping, deployment, and scalability, while PyTorch is more handy in research as it is more pythonic and easier to implement complex stuff. TensorFlow.js has several unique advantages over Python equivalent as it can run on the client side too, not just the server side (via Node) and on the server side it can potentially run faster than Python due to the JIT compiler of JS. Other than that the APIs etc are similar and Python is older so is more mature in terms of development.'"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "query = 'What are the differences between PyTorch and TensorFlow?'\n",
        "\n",
        "# create query vector\n",
        "res = openai.Embedding.create(\n",
        "    input=[query],\n",
        "    engine=embed_model\n",
        ")\n",
        "xq = res['data'][0]['embedding']\n",
        "\n",
        "# get relevant contexts\n",
        "res = index.query(vector=xq, top_k=10, include_metadata=True)\n",
        "contexts = [\n",
        "    x['metadata']['context'] for x in res['matches']\n",
        "]\n",
        "\n",
        "# build our prompt with the retrieved contexts included\n",
        "prompt_start = (\n",
        "    \"Give an exhaustive summary and answer based on the question using the contexts below.\\n\\n\"+\n",
        "    \"Context:\\n\"+\n",
        "    \"\\n\\n---\\n\\n\".join(contexts)+\"\\n\\n\"+\n",
        "    f\"Question: {query}\\n\"+\n",
        "    f\"Answer:\"\n",
        ")\n",
        "prompt_end = (\n",
        "    f\"\\n\\nQuestion: {query}\\nAnswer:\"\n",
        ")\n",
        "# append contexts until hitting limit\n",
        "for i in range(1, len(contexts)):\n",
        "    if len(\"\\n\\n---\\n\\n\".join(contexts[:i])) >= limit:\n",
        "        prompt = (\n",
        "            prompt_start +\n",
        "            \"\\n\\n---\\n\\n\".join(contexts[:i-1]) +\n",
        "            prompt_end\n",
        "        )\n",
        "    elif i == len(contexts):\n",
        "        prompt = (\n",
        "            prompt_start +\n",
        "            \"\\n\\n---\\n\\n\".join(contexts) +\n",
        "            prompt_end\n",
        "        )\n",
        "\n",
        "# now query text-davinci-003\n",
        "res = openai.Completion.create(\n",
        "    engine='text-davinci-003',\n",
        "    prompt=prompt,\n",
        "    temperature=0,\n",
        "    max_tokens=400,\n",
        "    top_p=1,\n",
        "    frequency_penalty=0,\n",
        "    presence_penalty=0,\n",
        "    stop=None\n",
        ")\n",
        "res['choices'][0]['text'].strip()"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "bH4B-C3A4Wy9"
      },
      "source": [
        "The advantage of Tensorflow.js could have been framed better and the fact that PyTorch has no equivalent explicitly stated. However, the answer is good and gives a nice summary and answer to our question — using information pulled from multiple sources."
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "p-e30MCcadOK"
      },
      "source": [
        "Once you're finished with the index we delete it to save resources:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "id": "2YWM7ASKadOL"
      },
      "outputs": [],
      "source": [
        "pinecone.delete_index(index_name)"
      ]
    },
    {
      "attachments": {},
      "cell_type": "markdown",
      "metadata": {
        "id": "L94VF1gHadOL"
      },
      "source": [
        "---"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.10.7 (main, Sep 14 2022, 22:38:23) [Clang 14.0.0 (clang-1400.0.29.102)]"
    },
    "vscode": {
      "interpreter": {
        "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
